package com.kye.express.business.jd.goods_all_category;

import android.databinding.DataBindingUtil;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.jakewharton.rxbinding2.view.RxView;
import com.kye.base.DebounceObservableTransformer;
import com.kye.express.R;
import com.kye.express.databinding.ItemJdAllcategoryBinding;
import com.kye.model.bean.jd.shop.JDGoodsCategoryBean;

import io.reactivex.functions.Consumer;
import me.drakeet.multitype.ItemViewBinder;

/**
 * 京东兑换-全部分类 ViewHolder
 * <p>
 * Created by SkyYu on 2018/1/31
 */
public class JDAllCategoryViewBinder extends ItemViewBinder<JDGoodsCategoryBean, JDAllCategoryViewBinder.ViewHolder> {

    private OnCategoryItemListener onItemListener;

    JDAllCategoryViewBinder(OnCategoryItemListener onItemListener) {
        this.onItemListener = onItemListener;
    }

    @NonNull
    @Override
    protected ViewHolder onCreateViewHolder(@NonNull LayoutInflater inflater, @NonNull ViewGroup parent) {
        View view = inflater.inflate(R.layout.item_jd_allcategory, parent, false);
        return new ViewHolder(view);
    }

    @Override
    protected void onBindViewHolder(@NonNull ViewHolder holder, @NonNull JDGoodsCategoryBean item) {
        holder.bindData(item);
    }

    public class ViewHolder extends RecyclerView.ViewHolder {

        private ItemJdAllcategoryBinding binding;
        private JDGoodsCategoryBean item;
        private View view;

        public ViewHolder(View itemView) {
            super(itemView);
            view = itemView;
            binding = DataBindingUtil.bind(itemView);
            RxView.clicks(binding.linearlayoutJdAllcategory)
                    .compose(new DebounceObservableTransformer<>())
                    .subscribe(new Consumer<Object>() {
                        @Override
                        public void accept(Object o) throws Exception {
                            if (onItemListener != null) {
                                onItemListener.onCategoryItemClicked(item);
                            }
                        }
                    });
        }

        public void bindData(JDGoodsCategoryBean item) {
            this.item = item;
            Glide.with(view.getContext())
                    .load(item.getImagePath())
                    .apply(new RequestOptions().error(R.drawable.error).placeholder(R.drawable.background_about_empty))
                    .into(binding.imageviewJdAllcategoryPic);
            binding.textviewJdAllcategoryTitle.setText(item.getClassName());
        }
    }

    public interface OnCategoryItemListener {
        void onCategoryItemClicked(JDGoodsCategoryBean item);
    }
}